package top.jiangqiang.qianyi.web.controller.admin;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import top.jiangqiang.qianyi.business.model.request.SysFileListRequest;
import top.jiangqiang.qianyi.business.model.request.SysFilePageRequest;
import top.jiangqiang.qianyi.business.model.request.SysFileSaveRequest;
import top.jiangqiang.qianyi.business.model.request.SysFileUpdateRequest;
import top.jiangqiang.qianyi.business.model.response.SysFileDetailResponse;
import top.jiangqiang.qianyi.business.model.response.SysFileListResponse;
import top.jiangqiang.qianyi.business.model.response.SysFilePageResponse;
import top.jiangqiang.qianyi.business.service.SysFileService;
import top.jiangqiang.qianyi.common.base.controller.BaseController;
import top.jiangqiang.qianyi.common.base.model.response.PageResult;
import top.jiangqiang.qianyi.common.base.model.response.Result;
import top.jiangqiang.qianyi.common.constants.GlobalConstants;
import top.jiangqiang.qianyi.common.constants.log.SysLogType;
import top.jiangqiang.qianyi.common.log.annotations.Log;

import java.util.List;

/**
 * <p>
 * 系统文件表 前端控制器
 * </p>
 *
 * @author JiangQiang
 * @since 2024-07-19
 */
@Tag(name = "文件相关操作接口", description = "")
@RestController
@RequiredArgsConstructor
@RequestMapping(GlobalConstants.AdminRequestURLConstants.ADMIN_FILE_CONTROLLER_PREFIX)
public class AdminSysFileController extends BaseController {
    private final SysFileService sysFileService;

    @Operation(summary = "上传文件")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Log(value = "上传文件", type = SysLogType.UPLOAD)
    public SseEmitter upload(@RequestParam("file") MultipartFile multipartFile) {
        return sysFileService.upload(multipartFile);
    }

    @Operation(summary = "获取文件访问路径", description = "不会校验文件是否存在")
    @GetMapping("/url/{*fileKey}")
    public Result<String> getUrl(@PathVariable @Length(min = 2, message = "fileKey不能为空") String fileKey) {
        return Result.ok(sysFileService.getUrl(fileKey));
    }

    @Operation(summary = "删除指定文件")
    @DeleteMapping("/url/{*fileKey}")
    public Result<Void> delete(@PathVariable @Length(min = 2, message = "fileKey不能为空") String fileKey) {
        sysFileService.delete(fileKey);
        return Result.ok();
    }

    @Operation(summary = "返回文件字节数组", description = "无论什么底层实现，皆可通过此接口访问对应文件")
    @GetMapping(value = "/download/{*fileKey}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> access(@PathVariable @Length(min = 2, message = "fileKey不能为空") String fileKey) {
        return ResponseEntity.ok(sysFileService.download(fileKey));
    }

    @GetMapping("/page")
    @Operation(summary = "系统文件表分页查询")
    @Log(value = "系统文件表分页查询", type = SysLogType.PAGE)
    public Result<PageResult<SysFilePageResponse>> page(@Validated @ParameterObject SysFilePageRequest request) {
        return sysFileService.page(request);
    }

    @GetMapping("/list")
    @Operation(summary = "系统文件表全表查询")
    @Log(value = "系统文件表全表查询", type = SysLogType.LIST)
    public Result<List<SysFileListResponse>> list(@Validated @ParameterObject SysFileListRequest request) {
        return sysFileService.list(request);
    }

    @PostMapping
    @Operation(summary = "系统文件表新增")
    @Log(value = "系统文件表新增", type = SysLogType.ADD)
    public Result<Void> save(@Validated SysFileSaveRequest request) {
        return sysFileService.add(request);
    }

    @PutMapping
    @Operation(summary = "系统文件表修改")
    @Log(value = "系统文件表修改", type = SysLogType.UPDATE)
    public Result<Void> update(@Validated SysFileUpdateRequest request) {
        return sysFileService.update(request);
    }

    @GetMapping("/{id}")
    @Operation(summary = "系统文件表详情")
    @Log(value = "系统文件表详情", type = SysLogType.DETAIL)
    public Result<SysFileDetailResponse> detail(@NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        return sysFileService.detail(id);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "系统文件表删除")
    @Log(value = "系统文件表删除", type = SysLogType.DELETE)
    public Result<Void> delete(@NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        return sysFileService.delete(id);
    }

}
